#pragma once
#include <string>
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include "Comm.hpp"
const int gdefaultid = -1;
const int gsize = 4096;
const std::string pathname = ".";
const int projid = 0x66;
const int gmode = 0666;
#define CREATER "creater"
#define USER "user"

class Shm
{
private:
    void Destory()
    {
        // if (_shmid == gdefaultid)
        //     return;
        Detach();
        int n = shmctl(_shmid, IPC_RMID, nullptr);
        if (_usertype == CREATER)
        {
            if (n > 0)
            {
                printf("shmctl delete shm %d sucess!\n", _shmid);
            }
            else
            {
                ERR_EXIT("shmctl");
            }
        }
    }
    void Detach()
    {
        int n = shmdt(_start_mem);
        if (n == 0)
        {
            printf("detach success\n");
        }
        else
        {
            ERR_EXIT("shmdt");
        }
    }
    void CreatHelper(int flg)
    {

        printf("key:0x%x\n", _key);
        //_shmid = shmget(k,_size,IPC_CREAT|IPC_EXCL|gmode);
        _shmid = shmget(_key, _size, flg);
        if (_shmid < 0)
        {
            ERR_EXIT("shmget");
        }
        printf("shmid %d\n", _shmid);
    }
    void Creat()
    {
        CreatHelper(IPC_CREAT | IPC_EXCL | gmode);
        /*key_t k = ftok(pathname.c_str(),projid);
        if(k < 0)
        {
            //perror("ftok");
            //exit(EXIT_FAILURE);
            ERR_EXIT("ftok");
        }
        printf("key:0x%x\n",k);
        _shmid = shmget(k,_size,IPC_CREAT|IPC_EXCL|gmode);
        if(_shmid < 0)
        {
            ERR_EXIT("shmget");
        }
        printf("shmid %d\n",_shmid);*/
    }
    void Attach()
    {
        _start_mem = shmat(_shmid, _start_mem, 0);
        if ((long long)_start_mem < 0)
        {
            ERR_EXIT("shmat");
        }
        printf("Attach Success\n");
    }
    void Get()
    {
        CreatHelper(IPC_CREAT); // 其实也可以创建共享内存，有存在bug的风险
        /*key_t k = ftok(pathname.c_str(),projid);
        if(k < 0)
        {
            //perror("ftok");
            //exit(EXIT_FAILURE);
            ERR_EXIT("ftok");
        }
        printf("key:0x%x\n",k);
        _shmid = shmget(k,_size,IPC_CREAT);
        if(_shmid < 0)
        {
            ERR_EXIT("shmget");
        }
        printf("shmid %d\n",_shmid);*/
    }

public:
    Shm(const std::string &pathname, int projid, const std::string usertype)
        : _shmid(gdefaultid), _size(gsize), _start_mem(nullptr), _usertype(usertype)
    {
        _key = ftok(pathname.c_str(), projid);
        if (_key < 0)
        {
            // perror("ftok");
            // exit(EXIT_FAILURE);
            ERR_EXIT("ftok");
        }
        if (_usertype == CREATER)
        {
            Creat();
        }
        else if (_usertype == USER)
            Get();
        else
        {
        }
        Attach();
    }
    // 一定创建全新的
    int Size()
    {
        return _size;
    }

    void *VirtualAddr()
    {
        printf("VirtualAddr %p\n", _start_mem);
        return _start_mem;
    }
    ~Shm()
    {
        std::cout << _usertype << std::endl;
        // if (_usertype == CREATER)
        Destory();
    }

private:
    int _shmid;
    int _size;
    void *_start_mem;
    std::string _usertype;
    key_t _key;
};